<template>
    <div class="menus-box">
        <div class="mod-meun">
            <el-form :inline="true" :model="dataForm">
                <el-form-item>
                    <el-button size="mini" type="primary" @click="addOrUpdateHandle(null, 3)">新增</el-button>
                </el-form-item>
            </el-form>
            <el-table
                row-key="menuId"
                border
                :data="dataList"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
                element-loading-text="拼命加载中"
                v-loading="dataListLoading"
                style="width: 100%"
            >
                <el-table-column label="菜单名称" width="200" prop="title"></el-table-column>
                <el-table-column prop="url" header-align="center" align="center" width="120" label="菜单地址"></el-table-column>
                <el-table-column prop="logo" header-align="center" align="center" label="菜单标识"></el-table-column>
                <el-table-column prop="type" header-align="center" align="center" label="类型">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.type === 1" size="small">菜单资源</el-tag>
                        <el-tag v-else size="small">页面资源</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="orderno"
                    header-align="center"
                    align="center"
                    width="150"
                    :show-overflow-tooltip="true"
                    label="排序号"
                ></el-table-column>
                <el-table-column fixed="right" header-align="center" align="center" width="330" label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="addOrUpdateHandle(scope.row, 1)">新增</el-button>
                        <el-button type="primary" size="mini" @click="addOrUpdateHandle(scope.row, 2)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="deleteHandle(scope.row.menuId)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateDom" @refreshDataList="getList"></add-or-update>
        </div>
    </div>
</template>
<script>
import addOrUpdate from './add-or-update';
import API from '@/api';
export default {
    components: {
        addOrUpdate
    },
    data() {
        return {
            tabPosition: 'left',
            dataForm: {},
            dataList: [],
            dataListLoading: false,
            addOrUpdateVisible: false
        };
    },
    mounted() {
        this.getList();
    },
    methods: {
        // 获取列表数据
        getList() {
            this.dataListLoading = true;
            API.menu.getMenuList().then((res) => {
                this.dataListLoading = false;
                this.dataList = res.result;
            });
        },
        // 新增或者编辑
        addOrUpdateHandle(items, type) {
            this.addOrUpdateVisible = true;
            this.$nextTick(() => {
                console.log(type);
                // 编辑
                if (type == 2) {
                    API.menu.lookMenuDetail({ menuId: items.menuId }).then((res) => {
                        console.log(res.result.title);
                        items['parentName'] = res.result.title;
                        this.$refs.addOrUpdateDom.init(items, type);
                    });
                } else {
                    this.$refs.addOrUpdateDom.init(null, type);
                }
            });
        },
        //删除菜单
        deleteHandle(id) {
            this.$alert('您确定要删除该条数据吗?', '', {
                confirmButtonText: '确定',
                callback: (action) => {
                    if (action !== 'cancel') {
                        API.menu.delMenu({ menuId: id }).then((res) => {
                            if (res.code == '0000') {
                                this.$message({
                                    type: 'success',
                                    message: `删除成功`
                                });
                                this.getList();
                            }
                        });
                    }
                }
            });
        }
    }
};
</script>
<style scoped>
.menus-box {
    background: #fff;
    padding: 20px;
}
</style>